package com.zkingsoft.zkingbase.model;

import com.zkingsoft.zkingbase.bean.User;
import com.zkingsoft.zkingbase.bean.resp.BaseResp;
import com.zkingsoft.zkingbase.bean.resp.MapInfoBean;
import com.zkingsoft.zkingbase.presenter.UserRegisterPresenter;
import com.zkingsoft.zkingbase.view.IPhoneRegistView;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Function;
import io.reactivex.schedulers.Schedulers;
import retrofit2.http.POST;
import retrofit2.http.Query;

/**
 * Created by ytf on 2017/3/6 006.
 * Description:
 */

public class UserRegisterModel extends BaseModel
{

    private Registerservice service;

    public void registerByPhone(String phone, String pwd, String varicode, final UserRegisterPresenter<IPhoneRegistView, User> presenter)
    {
        varicode(phone, pwd, varicode, presenter);
    }

    private void varicode(String phone,String pwd, String varicode,UserRegisterPresenter<IPhoneRegistView, User> presenter)
    {
        service = retrofit.create(Registerservice.class);
        observable = service.varificationRight(phone);
        observable.subscribeOn(Schedulers.io())
                .observeOn(Schedulers.io())
                .flatMap(new Function<BaseResp, ObservableSource<BaseResp>>()
                {
                    @Override
                    public ObservableSource<BaseResp> apply(BaseResp baseResp) throws Exception
                    {
                        //第一层成功
                        if(baseResp.info.equals("ok"))
                        {
                            return service.registByPhone(phone, pwd, varicode);
                        }else
                        {
                            //第一层失败，传递失败原因
                            return Observable.create(new ObservableOnSubscribe<BaseResp>()
                            {
                                @Override
                                public void subscribe(ObservableEmitter<BaseResp> e) throws Exception
                                {
                                    e.onError(new Throwable(baseResp.info));
                                }
                            });
                        }
                    }
                })
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BaseResp>()
                {
                    @Override
                    public void onSubscribe(Disposable d)
                    {
                        dis = d;
                        //开始
                        presenter.onStart();
                    }

                    @Override
                    public void onNext(BaseResp value)
                    {
                        MapInfoBean mi = value.mapInfo;
                        if(mi != null)
                        {
                            User u = mi.user;
                            if(u != null)
                            {
                                presenter.onSuccess(u, null);
                                dispose();
                            }
                        }else
                        {
                            presenter.onFailed(value.info, new Throwable("error"));
                        }
                    }

                    @Override
                    public void onError(Throwable e)
                    {
                        presenter.onFailed(getFailedString(e), e);
                        dispose();
                    }

                    @Override
                    public void onComplete()
                    {
                        presenter.onComplete();
                        dispose();
                    }
                });

    }

    public interface Registerservice
    {
        @POST("user/getRegisterCode?")
        Observable<BaseResp> varificationRight(@Query("phone") String phone);

        @POST("user/registByPhone?")
        Observable<BaseResp> registByPhone(
                @Query("suTel") String phone,
                @Query("suPassword") String password,
                @Query("yzm") String varicode);
    }
}

